package app.sceledroid.network;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;
import android.os.AsyncTask;
import app.sceledroid.entity.Deadline;
import app.sceledroid.services.SceleService;

public class Uploader extends AsyncTask<Deadline, Void, Boolean> {

	
	private Context context;
	private DefaultHttpClient client;
	private SceleService sceleService;
	private String filename;

	
	public Uploader (DefaultHttpClient client, String filename) {
		this.client = client;
		this.filename = filename;
	
	}
	
	@Override
	protected Boolean doInBackground(Deadline... arg0) {
		// TODO Auto-generated method stub
		
		boolean uploaded = false;
		String a[] = arg0[0].getUrl().split("=");
		
		String urlUpload = "http://scele.cs.ui.ac.id/mod/assignment/upload.php";
		File file = new File(filename);
		MultipartEntity datas = new MultipartEntity();
	    ContentBody fileStream = new FileBody(file);
	    datas.addPart("newfile",fileStream);
	    try {
			datas.addPart("id", new StringBody(a[1]));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
			datas.addPart("MAX_FILE_SIZE", new StringBody("2097152"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
			datas.addPart("save", new StringBody("Upload this file"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpPost uploadRequest = new HttpPost(urlUpload);
		
		try {
			uploadRequest.setEntity(datas);
			HttpResponse response = client.execute(uploadRequest);
			//response.setEntity(datas);
			InputStream stream = response.getEntity().getContent();
			
			Document doc = Jsoup.parse(stream, "UTF-8", urlUpload);
			Element el = doc.select("h2.main").first();
			
			if(el.text().contains("uploaded successfully")){
				uploaded = true;
				return new Boolean(uploaded);
			}
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return false;
	}

}
